package com.yoku.user.search;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.yoku.user.model.Shop;
import com.yoku.user.model.User;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * Desc:
 * Date: 2016/5/26
 */
@Component
public class Esearch {

    @Autowired
    Client client;

    @Autowired
    ObjectMapper objectMapper;

    public Boolean indexUser(User user){
        try {
            //可以指定documentId或不指定自动生成id.
            IndexResponse indexResponse = client.prepareIndex("yoku", "user").setSource(objectMapper.writeValueAsString(user)).get();
            //findByDocumentId(indexResponse.getId());
            return indexResponse.isCreated();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return false;
        }
    }

    public Boolean deleteUser(String documentId){
        DeleteResponse deleteResponse = client.prepareDelete().setId(documentId).get();
        if (deleteResponse.isFound()){
            return Boolean.TRUE;
        }
        return false;
    }

    public Boolean updateUser(User user, String documentId){
        UpdateResponse updateResponse = client.prepareUpdate("yoku", "user", documentId).setDoc(user).get();
        return updateResponse.isCreated();
    }

    public User findByDocumentId(String id){
        GetResponse getResponse = client.prepareGet("yoku", "user", id + "," + id).get();
        try {
            User user = objectMapper.readValue(getResponse.getSourceAsString(), User.class);
            return user;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public Boolean indexShop(Shop shop){
        Shop shop1 = new Shop();
        Shop.Range range = new Shop.Range();
        range.setType("polygon");
        Double[][] d1 = new Double[5][2];
        d1[0][0] = 0D;
        d1[0][1] = 0D;
        d1[1][0] = 0D;
        d1[1][1] = 1D;
        d1[2][0] = 1D;
        d1[2][1] = 1D;
        d1[3][0] = 1D;
        d1[3][1] = 0D;
        d1[4][0] = 0D;
        d1[4][1] = 0D;

        List<Double[][]> locationas = Lists.newArrayList();
        locationas.add(d1);
        range.setCoordinates(locationas);
        shop1.setName("xhj");
        shop1.setId(2L);

        Shop.Location location = new Shop.Location();

        Double[][] lonLat = new Double[1][2];
        lonLat[0][0] = 0D;
        lonLat[0][1] = 0D;
        location.setType("point");
        location.setLat(0D);
        location.setLon(0D);
        shop1.setLocation(location);
        shop1.setRange(range);
        try {
            String str = objectMapper.writeValueAsString(shop1);
            IndexResponse indexResponse = client.prepareIndex("yoku", "shop").setSource(str).get();

        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
